﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSNet.Data;
using WMSNet.Data.Repository;
using WMSNet.Model.Models;

namespace WMSNet.Core.Service
{
    public interface IClientService
    {
        /// <summary>
        /// Gets the specified client by number
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        Client GetByNumber(int number);

        /// <summary>
        /// Gets the specified client by name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        Client GetByName(string name);

        /// <summary>
        /// Creates a new client by the specified name
        /// </summary>
        /// <param name="name"></param>
        /// <param name="number"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        Client Create(string name, int number, string code);

        /// <summary>
        /// System client is the owner of the running WMS system, users assigned to the system client have have access to all warehouses regardless of client 
        /// partitioning
        /// </summary>
        /// <returns></returns>
        Client GetSystemClient();
    }

    public class ClientService : IClientService
    {
        private readonly IClientRepository _clientRepository;
        private readonly IUnitOfWork _unitOfWork;

        public ClientService(IClientRepository clientRepository, IUnitOfWork unitOfWork)
        {
            this._clientRepository = clientRepository;
            this._unitOfWork = unitOfWork;
        }

        public Client GetByNumber(int number)
        {
            return _clientRepository.Get(x => x.Cl_Nr == number);
        }

        public Client GetByName(string name)
        {
           return _clientRepository.Get(x => x.Name == name);
        }

        public Client Create(string name, int number, string code)
        {
            Client client = new Client()
            {
                 Name = name,
                 Cl_Nr = number,
                 Cl_Code = code
            };

            _clientRepository.Add(client);
            _unitOfWork.Commit();

            return client;
        }

        public Client GetSystemClient()
        {
            throw new NotImplementedException();
        }
    }
}
